import { login, reg } from "@/api/user"
import { delToken, getToken, setToken } from "@/utils/storage"

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  getters: {},
  mutations: {
    saveToken (state, newToken) {
      setToken(newToken)
      state.token = newToken
    },
    removeTokne (state) {
      delToken()
      state.token = ''
    }
  },
  actions: {
    async loginAction (context, data) {
      const res = await login(data)
      if (res.data.token)
        context.commit('saveToken', res.data.token)
    },
    async regAction (context, data) {
      const res = await reg(data)
      return res
    }
  },
}